package com.iplatform.file.minio.core;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 签名工具类
 *
 * @author LInYI
 */
public class AutographUtils {

    @Autowired
    private static MinioClientHelper minioClientHelper;

    public static void presign(Collection<?> collection, String fieldName, Object presignClass) {
        if (collection == null) {
            return;
        }
        if (collection.size() < 1) {
            return;
        }
        for (Object listObj : collection) {
            persign(listObj, fieldName, presignClass);
        }
    }

    public static void preMapData(Map<String, String> map, String filedName) throws Exception {
        if (StringUtils.isNotBlank(map.get(filedName))) {
            map.put(filedName,
                    minioClientHelper.presignedGetObject(map.get(filedName), null, null));
        }
    }


    public static void persign(Object object, String fieldName, Object presignClass) {
        try {
            if (object == null) {
                return;
            }
            if (org.apache.commons.lang3.StringUtils.isBlank(fieldName)) {
                return;
            }
            Field field = getField(object.getClass(), fieldName);
            if (field == null) {
                return;
            }
            field.setAccessible(true);

            Object fieldObj = field.get(object);
            if (fieldObj instanceof List) {
                presign((Collection<Object>) fieldObj);
                return;
            }
            if (fieldObj == null) {
                return;
            }
            if (presignClass != object.getClass()) {
                return;
            }
            if (fieldObj instanceof String) {
                String persignedUrl = minioClientHelper.presignedGetObject((String) fieldObj, null, null);
                field.set(object, persignedUrl);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void presign(Collection<Object> collection) {
        if (collection == null) {
            return;
        }
        if (collection.size() < 1) {
            return;
        }
        Iterator iterator = collection.iterator();
        try {
            Collection<Object> collect = collection.getClass().newInstance();
            while (iterator.hasNext()) {
                Object obj = iterator.next();
                if (obj instanceof String) {
                    try {
                        collect.add(minioClientHelper.presignedGetObject((String) obj, null, null));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            collection.clear();
            collection.addAll(collect);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Field getField(Class clazz, String fieldName) {
        if (clazz == Object.class) {
            return null;
        }
        try {
            return clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            return getField(clazz.getSuperclass(), fieldName);
        }
    }
}
